package vn.edu.saigontech.ncms.web.student;

import java.util.Collection;
import vn.edu.saigontech.ncms.model.enumeration.Gender;
import vn.edu.saigontech.ncms.model.info.Student;
import vn.edu.saigontech.ncms.service.StudentService;
import vn.edu.saigontech.ncms.util.DateUtil;
import vn.edu.saigontech.ncms.util.Checker;
import vn.edu.saigontech.ncms.web.AbstractValidator;

@SuppressWarnings("serial")
public class ValidateStudentAction extends AbstractValidator {
	
	private Long id;
	private String firstName;
	private String lastName;
	private String address;
	private String gender;
	private String dateOfBirth;
	private String email;
	private String consultedDate;
	private String testDate;
	private Long programId;
	private Long placementShiftId;
	private Long consultantId;
	private String phoneNumber;
	
	private Long[] surveyInfos;

	public String check() {
		Student student = StudentService.INSTANCE.findById(this.id); 
		if(!Checker.isNull(this.id)) {
			if(Checker.isNull(student)) {
				this.sendKeyError("validator.nonExist", "student");
			}
			if(student.getVersion() != this.version) {
				this.sendKeyError("validator.concurrency", "student");
			}
		}
		if(Checker.isEmpty(this.firstName)) {
			this.sendKeyError("validator.required", "firstName");
		}
		if(Checker.isEmpty(this.address)) {
			this.sendKeyError("validator.required", "address");
		}
		if(Checker.isEmpty(this.lastName)) {
			this.sendKeyError("validator.required", "lastName");
		}
		if(Checker.isEmpty(this.gender)) {
			this.sendKeyError("validator.required", "gender");
		}
		if(Checker.isEmpty(this.dateOfBirth)) {
			this.sendKeyError("validator.required", "dateOfBirth");
		}		
		if(!Checker.isDate(this.dateOfBirth, this.i18n("format.date"))) {
			this.sendKeyError("validator.mask", "dateOfBirth", "format.date");
		}
		if(!Checker.isEmpty(this.email)) {
			if(!Checker.isEmail(this.email)) {
				this.sendKeyError("validator.invalid", "email");
			}
		}
		if(Checker.isEmpty(this.phoneNumber)) {
			this.sendKeyError("validator.required", "phoneNumber");
		}
		if(Checker.isNull(this.id)) {
			Student studentSearch = new Student();
			studentSearch.setFirstName(this.firstName);
			studentSearch.setLastName(this.lastName);
			studentSearch.setGender(Gender.create(this.gender));
			studentSearch.setDateOfBirth(DateUtil.fromString(this.dateOfBirth, this.i18n("format.date")));
			Collection<Student> students = StudentService.INSTANCE.find(studentSearch);
			if(!students.isEmpty())
				this.sendKeyError("validator.exist", "student");
		}
		if(!Checker.isEmpty(this.consultedDate)) {
			if(!Checker.isDate(this.consultedDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "consultedDate", "format.date");
			}
		}
		else {
			this.sendKeyError("validator.required", "consultedDate");
		}
		if(Checker.isNull(this.programId)) {
			this.sendKeyError("validator.required", "program");
		}
//		if(Checker.isEmpty(this.testDate)) {
//			this.sendKeyError("validator.required", "testDate");
//		}
		if(!Checker.isEmpty(this.testDate)) {
			if(!Checker.isDate(this.testDate, this.i18n("format.date"))) {
				this.sendKeyError("validator.mask", "testDate", "format.date");
			}
		}
		if(Checker.isEmpty(this.surveyInfos)) {
			this.sendKeyError("validator.required", "surveyInfo");
		}
		if(Checker.isNull(this.consultantId)) {
			this.sendKeyError("validator.required", "consultant");
		}
		return this.toSuccess();
	}

	public String getDateOfBirth() {
		return dateOfBirth;
	}

	public void setDateOfBirth(String dateOfBirth) {
		this.dateOfBirth = dateOfBirth;
	}

	public String getFirstName() {
		return firstName;
	}

	public void setFirstName(String firstName) {
		this.firstName = firstName;
	}

	public String getLastName() {
		return lastName;
	}

	public void setLastName(String lastName) {
		this.lastName = lastName;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getGender() {
		return gender;
	}

	public void setGender(String gender) {
		this.gender = gender;
	}

	public String getConsultedDate() {
		return consultedDate;
	}

	public void setConsultedDate(String consultedDate) {
		this.consultedDate = consultedDate;
	}

	public String getTestDate() {
		return testDate;
	}

	public void setTestDate(String testDate) {
		this.testDate = testDate;
	}

	public Long getProgramId() {
		return programId;
	}

	public void setProgramId(Long programId) {
		this.programId = programId;
	}

	public Long getConsultantId() {
		return consultantId;
	}

	public void setConsultantId(Long consultantId) {
		this.consultantId = consultantId;
	}

	public String getPhoneNumber() {
		return phoneNumber;
	}

	public void setPhoneNumber(String phoneNumber) {
		this.phoneNumber = phoneNumber;
	}

	public Long getPlacementShiftId() {
		return placementShiftId;
	}

	public void setPlacementShiftId(Long placementShiftId) {
		this.placementShiftId = placementShiftId;
	}
	
	public Long[] getSurveyInfos() {
		return surveyInfos;
	}

	public void setSurveyInfos(Long[] surveyInfos) {
		this.surveyInfos = surveyInfos;
	}
	
	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}
}